package org.groupg.filetodb.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import org.apache.commons.csv.CSVFormat;
import org.groupg.filetodb.model.database.Table;
import org.groupg.filetodb.util.DatabaseMetadataUtil;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

public class TableConfig {
    /**
     * 要处理的表名称
     */
    private String tableName;
    /**
     * 要处理表的数据库说明
     */
    private String tableComment;
    /**
     * 要处理的表的其他说明
     */
    private String tableDescription;
    /**
     * 用来截断字段的配置
     */
    private String fieldSeparator = CSVFormat.DEFAULT.getDelimiterString();
    /**
     * 用来截断记录的配置
     */
    private String recordSeparator = CSVFormat.DEFAULT.getRecordSeparator();
    /**
     * 文件的字符集
     */
    private Charset charset = StandardCharsets.UTF_8;

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getTableComment() {
        return tableComment;
    }

    public void setTableComment(String tableComment) {
        this.tableComment = tableComment;
    }

    public String getTableDescription() {
        return tableDescription;
    }

    public void setTableDescription(String tableDescription) {
        this.tableDescription = tableDescription;
    }

    public String getFieldSeparator() {
        return fieldSeparator;
    }

    public void setFieldSeparator(String fieldSeparator) {
        this.fieldSeparator = fieldSeparator;
    }

    public String getRecordSeparator() {
        return recordSeparator;
    }

    public void setRecordSeparator(String recordSeparator) {
        this.recordSeparator = recordSeparator;
    }

    public Charset getCharset() {
        return charset;
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("tableName", tableName)
                .add("tableComment", tableComment)
                .add("tableDescription", tableDescription)
                .add("fieldSeparator", fieldSeparator)
                .add("recordSeparator", recordSeparator)
                .add("charset", charset)
                .toString();
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || getClass() != o.getClass()) return false;
        TableConfig that = (TableConfig) o;
        return Objects.equal(getTableName(), that.getTableName()) && Objects.equal(getTableComment(), that.getTableComment()) && Objects.equal(getTableDescription(), that.getTableDescription()) && Objects.equal(getFieldSeparator(), that.getFieldSeparator()) && Objects.equal(getRecordSeparator(), that.getRecordSeparator()) && Objects.equal(getCharset(), that.getCharset());
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(getTableName(), getTableComment(), getTableDescription(), getFieldSeparator(), getRecordSeparator(), getCharset());
    }

    public void setCharset(Charset charset) {
        this.charset = charset;
    }

    public Table getDBTable(DatabaseMetaData metaData) throws SQLException {
        return DatabaseMetadataUtil.loadTables(metaData).stream().filter(a->this.tableName.equalsIgnoreCase(a.getTableName())).findFirst().get();
    }
}
